/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header$
 * $Id$
 * $Author$
 * $Date$
 * $Revision$
 */
package com.elancom.framework.component;

import com.elancom.framework.commons.limit.Limit;
import com.elancom.framework.commons.limit.LimitImpl;

/**
 * @author elancom
 * @date 2012-8-25
 */
public abstract class ComponentUtils {
	public static Limit getLimit(Input input) {
		if (!hasLimit(input)) {
			return null;
		}

		Limit limit = new LimitImpl(getCurPage(input), getPageSize(input));
		return limit;
	}

	public static Integer getCurPage(Input input) {
		if (!hasLimit(input)) {
			return -1;
		}

		Integer curPage = null;
		Object curPageObj = input.getParameter(ComponentConstants.LIMIT_CUR_PAGE);

		if (curPageObj instanceof String) {
			curPageObj = Integer.valueOf(curPageObj.toString());
		} else if (curPageObj instanceof Integer) {
			curPage = (Integer) curPageObj;
		} else {
			throw new RuntimeException("非法当前页数据");
		}

		return curPage;
	}

	public static Integer getPageSize(Input input) {
		Integer pageSize = null;
		Object pageSizeObj = input.getParameter(ComponentConstants.LIMIT_PAGE_SIZE);

		if (pageSizeObj instanceof String) {
			pageSizeObj = Integer.valueOf(pageSizeObj.toString());
		} else if (pageSizeObj instanceof Integer) {
			pageSize = (Integer) pageSizeObj;
		} else {
			throw new RuntimeException("非法当前页数据");
		}

		return pageSize;
	}

	public static boolean hasLimit(Input input) {
		return input.getParameter(ComponentConstants.LIMIT_CUR_PAGE) != null;
	}
}
